package org.lex.app.sessions;

import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;

import org.lex.awt.Screen;
import org.lex.utils.Codec;


public class WindowSession extends AbstractSession {
	private static final String WINDOW_BOUNDS = "bounds";
	private static final String ALWAYS_ON_TOP = "ontop";

	@Override
	public WindowSession getComponentProperties(Component c) {
		this.clear();
		Window w = (Window) c;
		this.putProperty(WINDOW_BOUNDS, Codec.encodeRectangle(w.getBounds()));
		if (w.isAlwaysOnTopSupported())
			this.putProperty(ALWAYS_ON_TOP, Boolean.toString(((Window) c).isAlwaysOnTop()));
		return this;
	}

	@Override
	public void restoreComponent(Component component) {
		Window w = (Window) component;
		Rectangle rect = null;
		String bounds = this.getProperty(WINDOW_BOUNDS);
		if (null != bounds)
			rect = Codec.decodeRectangle(bounds);
		else {
			Point c = Screen.getCenterPoint();
			rect = new Rectangle(c.x / 2, c.y / 2, c.x, c.y);
		}
		w.setBounds(rect);
		if (w.isAlwaysOnTopSupported()) {
			w.setAlwaysOnTop(Boolean.parseBoolean(this.getProperty(ALWAYS_ON_TOP)));
		}
	}
}
